package hu.diploma.web.client.pages;

import hu.diploma.web.client.WidgetFactory;
import hu.diploma.web.client.service.WebUserService;
import hu.diploma.web.client.service.WebUserServiceAsync;
import hu.diploma.web.shared.webvos.UserWebVo;

import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.widget.Window;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.FieldSet;
import com.extjs.gxt.ui.client.widget.form.FormPanel;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.layout.FlowLayout;
import com.extjs.gxt.ui.client.widget.layout.FormLayout;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

public class Login extends VerticalPanel {
	private static Widget instanse;
	private static UserWebVo actUser = null;
	private UserWebVo newUserWebVo;
	public WebUserServiceAsync userService = GWT.create(WebUserService.class);

	private Login() {

		final FormPanel formLogin = new FormPanel();
		formLogin.setFrame(true);
		formLogin.setHeading("Bejelenkezés");
		formLogin.setWidth(470);
		formLogin.setLayout(new FlowLayout());

		FieldSet fieldSet = new FieldSet();
		fieldSet.setHeading("Adatok");

		FormLayout layout = new FormLayout();
		layout.setLabelWidth(75);
		fieldSet.setLayout(layout);

		final TextField<String> name = new TextField<String>();
		name.setFieldLabel("Név");
		name.setAllowBlank(false);
		fieldSet.add(name);

		final TextField password = new TextField();
		password.setFieldLabel("Jelszó");
		password.setPassword(true);
		password.setAllowBlank(false);
		fieldSet.add(password);

		formLogin.add(fieldSet);

		Button login = new Button("Belépés");
		Button registration = new Button("Regisztrálás");

		formLogin.addButton(login);
		formLogin.addButton(registration);
		this.add(formLogin);

		login.addSelectionListener(new SelectionListener<ButtonEvent>() {

			@Override
			public void componentSelected(ButtonEvent ce) {

				userService.login(getActUser(), new AsyncCallback<Boolean>() {

					public void onSuccess(Boolean result) {
						WidgetFactory.getCenterPanel().setWidget(OpenForm.getWidget());
						WidgetFactory.getMenu().initWidget();
						WidgetFactory.getHeader().initWidget();

					}

					public void onFailure(Throwable caught) {
						// TODO Auto-generated method stub

					}
				});
			}

		});

		registration.addSelectionListener(new SelectionListener<ButtonEvent>() {

			@Override
			public void componentSelected(ButtonEvent ce) {
				final Window window = new Window();
				/*
				 * window.setSize(350, 400); window.setPlain(true); window.setHeading("Reg"); window.setLayout(new
				 * FitLayout());
				 */

				window.setFrame(true);
				window.setHeading("Regisztrálás");
				window.setWidth(350);
				window.setLayout(new FlowLayout());
				window.setAutoHeight(true);
				window.setAutoWidth(false);
				FormLayout layout = new FormLayout();
				layout.setLabelWidth(100);

				FieldSet fieldSet = new FieldSet();
				fieldSet.setHeading("Adatok");

				fieldSet.setLayout(layout);

				final TextField<String> nev = new TextField<String>();
				nev.setFieldLabel("Név");
				nev.setAllowBlank(false);
				fieldSet.add(nev);

				final TextField<String> lnev = new TextField<String>();
				lnev.setFieldLabel("Leánykori név");

				fieldSet.add(lnev);

				final TextField<String> cim = new TextField<String>();
				cim.setFieldLabel("Cím");
				cim.setAllowBlank(false);
				fieldSet.add(cim);

				final TextField<String> sz_szam = new TextField<String>();
				sz_szam.setFieldLabel("Szemelyi szám");
				sz_szam.setAllowBlank(false);
				fieldSet.add(sz_szam);

				final TextField<String> tel = new TextField<String>();
				tel.setFieldLabel("Telefon");
				tel.setAllowBlank(false);
				fieldSet.add(tel);

				final TextField<String> fax = new TextField<String>();
				fax.setFieldLabel("Fax");
				fieldSet.add(fax);

				final TextField<String> szamlaszam = new TextField<String>();
				szamlaszam.setFieldLabel("Szamlaszám");
				szamlaszam.setAllowBlank(false);
				fieldSet.add(szamlaszam);

				final TextField<String> email = new TextField<String>();
				email.setFieldLabel("Email");
				email.setAllowBlank(false);
				fieldSet.add(email);

				final TextField<String> jelszo1 = new TextField<String>();
				jelszo1.setPassword(true);
				jelszo1.setFieldLabel("Jelszó");
				jelszo1.setAllowBlank(false);
				fieldSet.add(jelszo1);

				final TextField<String> jelszo2 = new TextField<String>();
				jelszo2.setFieldLabel("Jelszó megint");
				jelszo2.setPassword(true);
				jelszo2.setAllowBlank(false);
				fieldSet.add(jelszo2);

				Button registration = new Button("Regisztrál");

				Button cancel = new Button("Mégse");
				cancel.addSelectionListener(new SelectionListener<ButtonEvent>() {

					@Override
					public void componentSelected(ButtonEvent ce) {
						window.close();

					}

				});
				registration.addSelectionListener(new SelectionListener<ButtonEvent>() {

					@Override
					public void componentSelected(ButtonEvent ce) {

					}

				});
				window.add(fieldSet);
				window.setButtonAlign(HorizontalAlignment.CENTER);
				window.addButton(registration);
				window.addButton(cancel);
				window.show();

			}
		});

	}

	public static Widget getWidget() {
		if (instanse == null) {
			instanse = new Login();
		}
		return instanse;
	}

	public static void setActUser(UserWebVo actUser) {
		Login.actUser = actUser;
	}

	public static UserWebVo getActUser() {
		return actUser;
	}

}